layui.define(['table','form'],function (exports) {
    var table = layui.table
        ,tableInit = null;
    var controller = {
        index:function () {
            tableInit = table.render({
                elem: '#table-page'
                ,url: "index"
                ,toolbar: "#toolbar"
                ,defaultToolbar:[]
                ,parseData: function(res){ //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.list //解析数据列表
                    };
                }
                ,response: {
                    statusCode: 1 //规定成功的状态码，默认：0
                }
                ,cellMinWidth:100
                ,cols: [[
                    {fixed:'left', minWidth:10, type: 'checkbox'}
                    ,{field:'id', title: 'ID', sort: true}
                    ,{field:'name', title: '文件名'}
                    ,{field:'path', title: '文件路径'}
                    ,{field:'full_path', title: '文件全路径'}
                    ,{field:'md5', title: '文件md5'}
                    ,{field:'type_text', title: '文件类型'}
                    ,{fixed: 'right', align:'center', toolbar: '#action', width:150}
                ]]
                ,page: true
            });

            //监听工具条
            table.on('tool(table-page)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

                if(layEvent === 'detail'){ //查看
                    controller.detail_file(data);
                } else if(layEvent === 'del'){ //删除
                    controller.delete_api(data.id);
                } else if(layEvent === 'edit'){ //编辑
                    controller.open_edit(data.id);
                } else if(layEvent === 'LAYTABLE_TIPS'){
                    layer.alert('Hi，头部工具栏扩展的右侧图标。');
                }
            });
            //监听事件
            table.on('toolbar(table-page)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'add':
                        controller.open_edit();
                        break;
                    case 'delete':
                        controller.delete_api(checkStatus.data);
                        break;
                }
            });
        },
        open_edit:function (id = 0) {
            var url = '';
            var title = '';
            if(id){
                url = '/admin/file/edit/id/'+id;
                title = '编辑';
            }else{
                url = '/admin/file/add';
                title = '添加';
            }
            parent.layer.open({
                type:2
                ,title:title
                ,content:url
                ,area:['50%','80%']
                ,maxmin:true
                ,end:function () {
                    tableInit.reload();
                }
                ,btn:'确定'
                ,btnAlign: 'c'
                ,yes:function (index,layero) {
                    parent.layer.close(index);
                }
            });
        },
        add:function(){
            controller.edit();
        },
        edit:function () {
            layui.extend({
                upload_api: '{/}/static/util/upload_api',
            }).use(['upload_api'], function (upload_api) {
                upload_api.render('upload_img');
            });

        },
        delete_api:function (data) {
            layer.confirm("删除后无法恢复!您真的要删除吗？",function (index) {
                layer.close(index);
                var ids = [];
                if(data instanceof Array){
                    layui.each(data,function (k,item) {
                        ids.push(item.id);
                    });
                }else{
                    ids = [data];
                }
                var load_index = layer.load();
                $.post("delete",{ids:ids})
                    .success(function (response) {
                        if(response.code){
                            notice.success(response.msg);
                            setTimeout(function () {
                                tableInit.reload();
                            },1000);
                        }else{
                            notice.error(response.msg);
                        }
                    })
                    .complete(function () {
                        layer.close(load_index);
                    });
            });
        },
        detail_file:function (data) {
            let json = {
                "title": "附件", //相册标题
                "id": data.id, //相册id
                "start": 0, //初始显示的图片序号，默认0
                "data": [   //相册包含的图片，数组格式
                    {
                        "alt": data.name,
                        "pid": data.id, //图片id
                        "src": data.path, //原图地址
                        "thumb": data.path //缩略图地址
                    }
                ]
            };
            layer.photos({
                photos: json
            });
        }
    };
    exports('admin_file',controller)
});